package hu.rsc.feeds.server;

import hu.rsc.feeds.client.data.GeneratedEntry;
import hu.rsc.feeds.client.data.GeneratedFeed;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.ProcessingInstruction;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Text;

public class AtomGenerator {
    Document doc = new Document();
    SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");

    public AtomGenerator(GeneratedFeed feed, boolean asc) {
	ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", "type='text/xsl' href='localhost:8888/feed.xsl'");
	//this.doc.addContent(pi);
	List<GeneratedEntry> result = new ArrayList<GeneratedEntry>();

	final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	final Query query = new Query(RssReaderServiceImpl.TABLE_GENERATED_ENTRY);
	query.addFilter(GeneratedEntry.PROP_FEED, Query.FilterOperator.EQUAL, feed.getId());
	query.addSort(GeneratedEntry.PROP_PUB_DATE, asc ? SortDirection.ASCENDING : SortDirection.DESCENDING);
	PreparedQuery pq = datastore.prepare(query);
	for (Entity e : pq.asIterable(FetchOptions.Builder.withOffset(0).limit(15))) {
	    GeneratedEntry gf = new GeneratedEntry();
	    gf.setDescription(((Text) e.getProperty(GeneratedEntry.PROP_DESC)).getValue());
	    gf.setTitle((String) e.getProperty(GeneratedEntry.PROP_TITLE));
	    gf.setLink((String) e.getProperty(GeneratedEntry.PROP_LINK));
	    gf.setFeed((Long) e.getProperty(GeneratedEntry.PROP_FEED));
	    gf.setPubdate((Date) e.getProperty(GeneratedEntry.PROP_PUB_DATE));
	    gf.setId(e.getKey().getId());
	    result.add(gf);
	}

	Element root = new Element("feed");
	Namespace NS = Namespace.getNamespace("http://www.w3.org/2005/Atom");
	Namespace NAMESPACE = Namespace.getNamespace("blh", "http://blog.hu/2007/bloghurss/");
	root.setNamespace(NS);
	//root.addNamespaceDeclaration(NS);
	//root.addNamespaceDeclaration(NAMESPACE);
	this.doc.setRootElement(root);

	Element title = new Element("title");
	title.setNamespace(NS);
	title.setText(feed.getTitle());
	root.addContent(title);

	Element link = new Element("link");
	link.setNamespace(NS);
	link.setAttribute("href", "http://rscrssreader.appspot.com");
	//link.setAttribute("rel", "self");
	root.addContent(link);

	Element id = new Element("id");
	id.setNamespace(NS);
	id.setText("http://rscrssreader.appspot.com/" + feed.getId());
	root.addContent(id);

	Element updated = new Element("updated");
	updated.setNamespace(NS);
	//updated.setText("2011-09-24T15:31:25+02:00");
	updated.setText(this.SDF.format(new Date()) + "T15:31:25+02:00");
	root.addContent(updated);

	Element generator = new Element("generator");
	generator.setNamespace(NS);
	generator.setText("Rsc Rssfeeds");
	root.addContent(generator);

	Element rights = new Element("rights");
	rights.setNamespace(NS);
	rights.setText("©2011 Red Staff Corporation");
	root.addContent(rights);

	for (GeneratedEntry e : result) {

	    Element entry = new Element("entry");
	    entry.setNamespace(NS);

	    Element eid = new Element("id");
	    eid.setNamespace(NS);
	    eid.setText("http://rscrssreader.appspot.com/a" + e.getId());
	    entry.addContent(eid);

	    Element etitle = new Element("title");
	    etitle.setNamespace(NS);
	    etitle.setAttribute("type", "text");
	    etitle.setText(e.getTitle());
	    entry.addContent(etitle);

	    Element eupdated = new Element("updated");
	    eupdated.setNamespace(NS);
	    eupdated.setText(this.SDF.format(e.getPubdate()) + "T" + this.TIME_FORMAT.format(e.getPubdate()) + "+02:00");
	    entry.addContent(eupdated);

	    Element published = new Element("published");
	    published.setNamespace(NS);
	    published.setText(this.SDF.format(e.getPubdate()) + "T15:31:25+02:00");
	    entry.addContent(published);

	    Element author = new Element("author");
	    author.setNamespace(NS);
	    Element name = new Element("name");
	    name.setNamespace(NS);
	    name.setText(feed.getAuthor());
	    author.addContent(name);
	    Element email = new Element("email");
	    email.setNamespace(NS);
	    email.setText(RssReaderServiceImpl.SENDER);
	    author.addContent(email);
	    entry.addContent(author);

	    Element elink = new Element("link");
	    elink.setNamespace(NS);
	    elink.setAttribute("href", e.getLink() != null ? e.getLink() : "http://rscrssreader.appspot.com");
	    entry.addContent(elink);

	    CDATA cdata = new CDATA(e.getDescription());
	    Element content = new Element("content");
	    content.setNamespace(NS);
	    content.setAttribute("type", "html");
	    //content.setText(e.getDescription());
	    content.setContent(cdata);
	    entry.addContent(content);

	    root.addContent(entry);

	}
    }

    public Document getDocument() {
	return this.doc;
    }

}
